package cn.com.sd.system.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import cn.com.sd.common.util.Page;
import cn.com.sd.system.dao.IRoleDAO;
import cn.com.sd.system.model.Role;
import cn.com.sd.system.service.IRoleService;

@Transactional
@Component(value = "roleService")
public class RoleServiceImpl implements IRoleService{

	@Resource(name = "roleDAO")
	private IRoleDAO roleDAO;
	
	@Override
	public List<Role> list(Page page) {
		return roleDAO.getRoleList(page);
	}

	@Override
	public Role detail(Integer id) {
		Role role = roleDAO.getObject(id);
		if (role == null) {
			return null;
		}
		return role;
	}
	
	@Override
	public Boolean saveOrUpdate(Role role) {
		try {
			roleDAO.saveObject(role);
			return true;
			
		} catch (Exception e) {
			return false;
		}
	}
	
	@Override
	public Boolean del(Integer id) {
		try {
			Role role = roleDAO.getObject(id);
			if (role == null) {
				return true;
			}
			roleDAO.deleteObject(role);
			return true;
			
		} catch (Exception e) {
			return false;
		}
	}

	@Override
	public Boolean delBanch(List<Integer> ids) {
		try {
			List<Role> roles = new ArrayList<Role>();
			for (Integer id : ids) {
				if (id == null || id <= 0) {
					continue;
				}
				Role temp = new Role();
				temp.setId(id);
				roles.add(temp);
			}
			roleDAO.deleteObjects(roles);
			return true;
			
		} catch (Exception e) {
			return false;
		}
	}

	@Override
	public Integer getRoleCount() {
		int count = roleDAO.getRoleCount();
		if (count <= 0) {
			return 0;
		}
		return count;
	}
}
